package com.kaiyidiannao.activity.menu_right;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.AppUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.data.Validate;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.req.model.GetJoinIn;
import com.taplinker.core.util.LogUtil;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by mark on 2014/4/17.
 */
public class Proxy extends BaseActionBarActivity implements Observer {



    private EditText sellerName,phone,name;
    private ImageButton positiveButton;

    private String seller,mobile,truename;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.proxy);
        // 添加观察者
        DataCenter.getInstance().addObserver(this);
        ById();
        AppUtils.setupUI(findViewById(R.id.proxy), this);

    }

    private void ById(){
        sellerName = (EditText)findViewById(R.id.sellerName);
        phone = (EditText)findViewById(R.id.phone);
        name = (EditText)findViewById(R.id.name);
        positiveButton = (ImageButton)findViewById(R.id.positiveButton);

        positiveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                seller = sellerName.getText().toString();
                mobile = phone.getText().toString();
                truename = name.getText().toString();
                if(seller != null && !"".equals(seller)){
                    if(seller.matches(Validate.sellerValidate)){
                        if(mobile != null && !"".equals(mobile)){
                            if(mobile.matches(Validate.phoneValidate)){
                                if(truename != null && !"".equals(truename)){
                                    if(Validate.truenameValidate(truename)){
                                        showProgressDialog("正在发送");
                                        new GetJoinIn("软件合作代理",seller,mobile,truename).execute();
                                    }else {
                                        AlertDialog builder = new AlertDialog.Builder(Proxy.this).setTitle("错误信息")
                                                .setMessage("姓名为2-5位，不能输入空格").setPositiveButton("确定", null).show();
                                    }

                                }else {
                                    AlertDialog builder = new AlertDialog.Builder(Proxy.this).setTitle("错误信息")
                                            .setMessage("姓名不能为空").setPositiveButton("确定", null).show();
                                }
                            }else {
                                AlertDialog builder = new AlertDialog.Builder(Proxy.this).setTitle("错误信息")
                                        .setMessage("电话输入不合法").setPositiveButton("确定", null).show();
                            }

                        }else {
                            AlertDialog builder = new AlertDialog.Builder(Proxy.this).setTitle("错误信息")
                                    .setMessage("电话不能为空").setPositiveButton("确定", null).show();
                        }
                    }else {
                        AlertDialog builder = new AlertDialog.Builder(Proxy.this).setTitle("错误信息")
                                .setMessage("店名中不能有空格").setPositiveButton("确定", null).show();
                    }

                }else {
                    AlertDialog builder = new AlertDialog.Builder(Proxy.this).setTitle("错误信息")
                            .setMessage("店名不能为空").setPositiveButton("确定", null).show();
                }
            }
        });
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        closeProgressDialog();
        if (flag == DataCenter.flag.GET_JOIN_IN_SUCCESS
                || flag == DataCenter.flag.GET_JOIN_IN_FAILED) { //类表数据

            String code = DataCenter.code;
            if (flag == DataCenter.flag.GET_JOIN_IN_SUCCESS) {
                if (code.equals(DataCenter.successFlag)) {
                    closeProgressDialog();
                    Toast.makeText(Proxy.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    closeProgressDialog();
                    Toast.makeText(Proxy.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    return;
                }
            }

            if (flag == DataCenter.flag.GET_JOIN_IN_FAILED) {
                closeProgressDialog();
                //Toast.makeText(Proxy.this, "failed", Toast.LENGTH_SHORT).show();
            }

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, Proxy.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }
}
